package com.back.campus_back.service.impl;

import com.back.campus_back.domain.Collect;
import com.back.campus_back.domain.Goods;
import com.back.campus_back.mapper.CollectMapper;
import com.back.campus_back.service.CollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-04-17
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {
    @Autowired
    CollectMapper collectMapper;


    //查看收藏商品的信息
    @Override
    public List<Goods> findAllCollect(@Param(value = "u_id")int u_id){
        return collectMapper.findAllCollect(u_id);
    }

    //个人添加收藏商品3
    @Override
    public String collectGoods(@Param(value = "u_id")int u_id,@Param(value = "g_id")int g_id){

        //添加收藏前查看是否已经收藏了
        HashMap<String, Object> map = new HashMap<>();
        map.put("u_id",u_id);
        map.put("g_id",g_id);
        List<Collect> collects = collectMapper.selectByMap(map);
        if (collects.size()!=0){
            return "请勿重复收藏";
        }

        int result = collectMapper.collectGoods(u_id,g_id);
        if(result == 1){
            return "收藏成功";
        }else{
            return "收藏失败";
        }
    }


    //个人删除收藏商品
    @Override
    public String cancelCollect(@Param(value = "u_id")int u_id,@Param(value = "g_id")int g_id){
        int result = collectMapper.cancelCollect(u_id,g_id);
        if(result == 1){
            return "取消收藏成功";
        }else{
            return "取消收藏失败";
        }
    }


}
